package com.zyp.weibo_demo1.controller;

import com.zyp.weibo_demo1.mapper.CommentMapper;
import com.zyp.weibo_demo1.pojo.dto.CommentDto;
import com.zyp.weibo_demo1.pojo.entity.Comment;
import com.zyp.weibo_demo1.pojo.vo.CommentVo;
import com.zyp.weibo_demo1.pojo.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/comment/")
public class CommentController {

    @Autowired
    CommentMapper mapper;

    @RequestMapping("insert")
    public int insert(@RequestBody CommentDto commentDto, HttpSession session) {
        UserVo u = (UserVo) session.getAttribute("user");
        if (u==null) {
            return 2;
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDto,comment);
        comment.setCreated(new Date());
        comment.setUserId(u.getId());
        mapper.insert(comment);
        return 1;
    }

    @RequestMapping("select")
    public List<CommentVo> select(int id) {
        return mapper.selectByWeiboId(id);
    }
}
